/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.DistritoLocalDao;
import edu.upc.clase.demo.entity.DistritoLocal;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Carlos
 */
@Repository
public class DistritoLocalDaoImpl extends JdbcDaoSupport implements DistritoLocalDao {
            @Autowired
    public DistritoLocalDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
     }

    @Override
    public void insertar(DistritoLocal distritolocal) {
        getJdbcTemplate().update(
                "insert into distritolocal (iddistrito,idlocal) values (?,?)",
                distritolocal.getIdDistrito(), distritolocal.getIdLocal());
    }

    @Override
    public void eliminar(DistritoLocal distritolocal) {
        getJdbcTemplate().update(
                "delete from distritolocal where iddistrito = ? and idlocal = ?",
                distritolocal.getIdDistrito(), distritolocal.getIdLocal());
    }

    @Override
    public DistritoLocal buscar(Integer iddistrito, Integer idlocal) {
            try {
            return getJdbcTemplate().queryForObject(
                    "select iddistrito, idlocal from distritoLocal where iddistrito=? and idlocal=?",
                    new BeanPropertyRowMapper<DistritoLocal>(DistritoLocal.class), iddistrito,idlocal);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
